
import { getLogin } from "@/api/common";
import { loadCookie, loadStorage, saveStorage } from '@/util/cache'
import { ACCESS_TOKEN, LOGIN_STATUS, USER_INFO, WX_CODE } from '@/util/constants'
export default defineStore('useCommon', {
	state: () => ({
		subMsgKey: "", // 定义通知(由于路由不再支持parmas传参丢失)
		goods_card_num: 0,
		is_reload_card: false,
		loginStatus: Number(loadCookie(LOGIN_STATUS, '0')),
		accessToken: loadStorage(ACCESS_TOKEN, ''),
		userInfo: loadStorage(USER_INFO, { username: '' }),
		wxCode: loadStorage(WX_CODE, ''),
		wxTicket: {
			timestamp: null,
			nonceStr: null,
			signature: null
		},

	}),
	getters: {},
	actions: {
		// 设置缓存页面通知事件标识
		SET_SUB_MSG_KEY(data) {
			this.subMsgKey = data;
		},
		// 设置购物车数量
		SET_GOODS_NUM(data) {
			this.goods_card_num = data;
		},
		// 是否重新加载购物车
		SET_RELAOD_SHOP(data) {
			this.is_reload_card = data;
		},
		SET_LOGIN_STATUS(data) {
			this.loginStatus = data;
		},
		SET_ACCESS_TOKEN(data) {
			this.accessToken = data;
			saveStorage(ACCESS_TOKEN, data)
		},
		SET_USER_INFO(data) {
			this.userInfo = data;
		},
		SET_WX_CODE(data) {
			this.wxCode = data;
			saveStorage(WX_CODE, data)
		},

		SET_WX_TICKET(data) {
			this.wxTicket = data;
		},
		// 登陆
		async getLogin(params) {
			let url = $globalRequestUrl({
				url: getLogin,
			});

			try {
				const rep = await $globalRequest(url, params, { method: "get" });
				return rep;
			} catch (error) {
				return error;
			}
		},
		async getTicket(params) {
			try {
				const rep = await $globalRequest('https://surveys.nuyuku.cn/api/wechat/getTicket', params, { method: "get" });
				return rep;
			} catch (error) {
				return error;
			}
		}
	},
});
